﻿using MediatR;
using SystemMHB.API.Application.Command.ProcessTaskCommand;
using SystemMHB.Domian.DTO;
using SystemMHB.Domian;
using SystemMHB.Infrstructer.Interface;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Tokens;

namespace SystemMHB.API.Application.CommandHandlers.ProcessTaskCommandHandler
{
    public class ProcessTaskShowCommandHandler:IRequestHandler<ProcessTaskShowCommand, PagingDTO<ProcessTask>>
    {
        /// <summary>
        /// 工序任务仓储接口
        /// </summary>
        IProcessTaskRepository _process;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_process">工序任务仓储接口</param>
        public ProcessTaskShowCommandHandler(IProcessTaskRepository _process)
        {
            this._process = _process;
        }

        public async Task<PagingDTO<ProcessTask>> Handle(ProcessTaskShowCommand request, CancellationToken cancellationToken)
        {
            var paging = new PagingDTO<ProcessTask>();
            var qouet = (from task in await _process.GetAllAsync() where task.IsDel==false select task);
            if (!string.IsNullOrEmpty(request.taskMumber))
            {
                qouet= qouet.Where(task => task.taskMumber.Contains(request.taskMumber));
            }
            if (request.taskStatus!=0)
            {
                qouet = qouet.Where(task => task.taskStatus == request.taskStatus);
            }
            paging.TotaCount = qouet.Count();
            paging.pageCount = (int)Math.Ceiling((paging.TotaCount * 1.0) / request.pageSzen);
            paging.plist = qouet.OrderBy(p => p.processTaskId).Skip((request.pageiden - 1) * request.pageSzen).Take(request.pageSzen).ToList();
            return paging;
        }
    }
}
